﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpShare.Configuration;
using System.Configuration;
using System.ComponentModel;
using SharpShare.Collections;

namespace SharpShare.Storage.ApplePhotoLibrary.Configuration {
    public class ApplePhotoLibraryStorageProviderConfigurationElement : StorageProviderElement {
        [ConfigurationProperty("baseStorageProvider", IsRequired = true)]
        [TypeConverter(typeof(StorageProviderNameConverter))]
        [DisplayName("Source Storage Provider")]
        public string BaseStorageProvider {
            get {
                return (string)this["baseStorageProvider"];
            }
            set {
                this["baseStorageProvider"] = value;
            }
        }

        [ConfigurationProperty("libraryPath", IsRequired = true)]
        [TypeConverter(typeof(StoragePathTypeConverter))]
        [DisplayName("iPhoto Library Location")]
        public Path<string> LibraryPath {
            get {
                return (Path<string>)this["libraryPath"];
            }
            set {
                this["libraryPath"] = value;
            }
        }

        public override IStorageProvider CreateStorageProvider(IConfigurationContext context) {
            IStorageProvider baseProvider = context.GetStorageProvider(this.BaseStorageProvider);
            IStorageContainer container = baseProvider.Locate(this.LibraryPath).DemandContainer();

            if (container == null) {
                return null;
            }

            PhotoLibraryStorageProvider provider = new PhotoLibraryStorageProvider(this.Name, container);

            return provider;
        }
    }
}
